#!/usr/bin/env python
# -*- coding:utf8 -*-

"""
@version: 
@author: lh
@license: Apache Licence 
@contact: liuhuan0672@gmail.com
@site: 
@software: PyCharm
@file: 0005.py.py
@time: 2016/1/20 14:21

第 0005 题：你有一个目录，装了很多照片，把它们的尺寸变成都不大于 iPhone5 分辨率（1136*640）的大小。
"""
from PIL import Image


def resize_image(image):
    im = Image.open(image)
    weight, height = im.size
    if weight > 1136 or height > 640:
        dw = weight / 1136
        dh = height / 640
        ds = max(dw, dh)
        new_weight = int(weight / ds)
        new_height = int(height / ds)
        im = im.resize((new_weight, new_height))
        print("Success to resize the image %s to %d*%d" % (image, new_weight, new_height))
        img_name = image.split('/')[-1]
        im.save('./result/' + img_name)
    else:
        print('The image %s does not need to be resized.' % image)


if __name__ == '__main__':
    resize_image('./static/0005.jpg')
